package com.flyduck.vueshop.order.manager;

import cn.hutool.core.collection.CollectionUtil;
import com.flyduck.vueshop.cart.feign.RemoteCartItemFeignClient;
import com.flyduck.vueshop.cart.vo.RemoteCartItemRespVO;
import com.flyduck.vueshop.common.core.exception.BusinessException;
import com.flyduck.vueshop.common.core.utils.BeanUtils;
import com.flyduck.vueshop.order.dto.CartItemDTO;
import com.flyduck.vueshop.product.feign.RemoteProductFeignClient;
import com.flyduck.vueshop.product.feign.RemoteSkuStockFeignClient;
import com.flyduck.vueshop.product.vo.RemoteProductRespVO;
import com.flyduck.vueshop.product.vo.RemoteSkuStockRespVO;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * AppCartItemManager
 * </p>
 *
 * @author flyduck
 * @since 2024-06-18
 */
@Component
public class AppCartItemManager {
    @Resource
    private RemoteCartItemFeignClient remoteCartItemFeignClient;
    @Resource
    private RemoteProductFeignClient remoteProductFeignClient;
    @Resource
    private RemoteSkuStockFeignClient remoteSkuStockFeignClient;

    //获取购物车的商品/立即购买的商品
    public List<CartItemDTO> getCartItemList(List<Long> cartIds, Long userId, Long productId, Long skuId, Integer quantity){
        List<CartItemDTO> cartItemList;
        if (CollectionUtil.isNotEmpty(cartIds)) {
            //购物车购买
            List<RemoteCartItemRespVO> remoteCartItemRespVOList = remoteCartItemFeignClient.getCartItemListByIds(cartIds).getData();
            cartItemList = BeanUtils.toBeanList(remoteCartItemRespVOList, CartItemDTO.class);
            for (CartItemDTO cartItemDTO : cartItemList) {
                RemoteProductRespVO remoteProductRespVO = remoteProductFeignClient.getProductById(cartItemDTO.getProductId()).getData();

                cartItemDTO.setProductName(remoteProductRespVO.getName());
                cartItemDTO.setProductImage(remoteProductRespVO.getImage());
                cartItemDTO.setProductSn(remoteProductRespVO.getSn());
                cartItemDTO.setCategoryId(remoteProductRespVO.getCategoryId());

                RemoteSkuStockRespVO remoteSkuStockRespVO = remoteSkuStockFeignClient.getSkuStock(cartItemDTO.getProductId(), cartItemDTO.getSkuId()).getData();
                cartItemDTO.setPrice(remoteSkuStockRespVO.getPrice());
                cartItemDTO.setSku(remoteSkuStockRespVO.getSku());
            }
        }else {
            //立即购买
            RemoteProductRespVO remoteProductRespVO = remoteProductFeignClient.getProductById(productId).getData();
            if (remoteProductRespVO == null || !remoteProductRespVO.getIsOnSale()) {
                throw new BusinessException("商品未上架");
            }

            CartItemDTO cartItemDTO = new CartItemDTO();
            cartItemDTO.setProductId(productId);
            cartItemDTO.setSkuId(skuId);
            cartItemDTO.setQuantity(quantity);
            cartItemDTO.setProductName(remoteProductRespVO.getName());
            cartItemDTO.setProductImage(remoteProductRespVO.getImage());
            cartItemDTO.setProductSn(remoteProductRespVO.getSn());
            cartItemDTO.setCategoryId(remoteProductRespVO.getCategoryId());
            cartItemDTO.setUserId(userId);

            RemoteSkuStockRespVO remoteSkuStockRespVO = remoteSkuStockFeignClient.getSkuStock(cartItemDTO.getProductId(), cartItemDTO.getSkuId()).getData();

            cartItemDTO.setPrice(remoteSkuStockRespVO.getPrice());
            cartItemDTO.setSku(remoteSkuStockRespVO.getSku());

            cartItemList = new ArrayList<>();
            cartItemList.add(cartItemDTO);
        }
        return cartItemList;
    }
}
